//
//  AppDelegate.swift
//  weiboLQ
//
//  Created by 半缘君 on 15/12/6.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
//        print(UserAccountViewModel.shareAccountModel.account)
        
        setUpAppearance()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
//        let btn = UIButton(frame: CGRect(x: 300, y: 600, width: 50, height: 100))
//        btn.backgroundColor = UIColor.redColor()
//        
//        window?.addSubview(btn)

        
        window?.rootViewController = defaultRootViewController
        
        window?.makeKeyAndVisible()
        
        NSNotificationCenter.defaultCenter().addObserverForName(LQChangeRootViewController, object: nil, queue: nil) { (notification) -> Void in
            self.window?.rootViewController = (notification.object as? String != "main") ? WelcomViewController() : MainViewController()
        }
        
        return true
    }
    func applicationDidEnterBackground(application: UIApplication) {
        StatusDAL.clearDataCache()
    }
    
    func setUpAppearance(){
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }
}

extension AppDelegate {
    
    private var defaultRootViewController:UIViewController {
        if UserAccountViewModel.shareAccountModel.isLogin {
            return isNewVersion ? NewFeatureViewController() : WelcomViewController()
        }
        return isNewVersion ? NewFeatureViewController() : MainViewController()
//    return MainViewController()
    }
    private var isNewVersion:Bool {
        //获取当前版本
        let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        //从本地获取上一个版本
        let preVersion:String? = NSUserDefaults.standardUserDefaults().stringForKey("version")

        //记录上一个版本
        NSUserDefaults.standardUserDefaults().setObject(version, forKey: "version")
        return version > preVersion
    }
}

